﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace System.Security.Cryptography
{
    public static class MD5Extension
    {
        public static string ComputeHash(this MD5 md5, string pass)
        {
            byte[] data = md5.ComputeHash(Encoding.Default.GetBytes(pass));

            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            return sBuilder.ToString();
        }

        public static string ComputeHash(this MD5 md5, string pass, string salt)
        {
            byte[] bytespass = Encoding.Default.GetBytes(pass+salt);
            for (int i = 0; i < bytespass.Length-5; i++)
            {
                if (i % 7 == 0)
                {
                    byte b = bytespass[i];
                    bytespass[i] = bytespass[i + 2];
                    bytespass[i + 2] = b;
                }
            }


            byte[] data = md5.ComputeHash(bytespass);

            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            return sBuilder.ToString();
        }
    }

    public static class SHA1Extension
    {
        public static string ComputeHash(this SHA1 sha1, string pass)
        {
            byte[] data = sha1.ComputeHash(Encoding.Default.GetBytes(pass));

            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            return sBuilder.ToString();
        }

        public static string ComputeHash(this SHA1 sha1, string pass, string salt)
        {
            byte[] bytespass = Encoding.Default.GetBytes(pass + salt);
            for (int i = 0; i < bytespass.Length - 5; i++)
            {
                if (i % 7 == 0)
                {
                    byte b = bytespass[i];
                    bytespass[i] = bytespass[i + 2];
                    bytespass[i + 2] = b;
                }
            }


            byte[] data = sha1.ComputeHash(bytespass);

            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            return sBuilder.ToString();
        }
    }
}
